package com.qfxl.common.config.webMvcConfig;

/**
 * @Title: TokenValidationListenerProcessor
 * @Author cj
 * @Package com.qfxl.common.config.webMvcConfig
 * @Date 2025/8/26 16:58
 * @description: token校验处理器
 */

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.List;

/**
 * @author cj
 * @version 1.0
 * @description: TODO
 * @date 2025/8/26 16:58
 */
@Service
public class TokenValidationListenerProcessor {
    private final List<TokenValidationListener> tokenValidationListeners;

    // Spring会自动将所有TokenValidationListener实现注入到List中
    public TokenValidationListenerProcessor(List<TokenValidationListener> paymentServices) {
        this.tokenValidationListeners = paymentServices;
    }

    //     tokenValidationListener.beforeValidation(request, response, handler);
    public void beforeValidation(HttpServletRequest request, HttpServletResponse response, Object handler) {
        if (!CollectionUtils.isEmpty(tokenValidationListeners)) {
            for (TokenValidationListener tokenValidationListener : tokenValidationListeners) {
                tokenValidationListener.beforeValidation(request, response, handler);
            }
        }
    }

    boolean isSkipValidation(HttpServletRequest request, HttpServletResponse response, Object handler) {
        if (!CollectionUtils.isEmpty(tokenValidationListeners)) {
            for (TokenValidationListener tokenValidationListener : tokenValidationListeners) {
                boolean skipValidation = tokenValidationListener.isSkipValidation(request, response, handler);
                if (skipValidation) {
                    return true;
                }
            }
        }
        return false;
    }
}
